<?php

namespace Admin\Frontend\Controllers;

use Helpers\Assets;
use Helpers\Template;
use Services\Msg\Msg;

class RemindController extends ControllerBase
{

    /**
     * 获取未读消息（首页使用）
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by ck 20190118
     */
//    public function getUnreadNoticeAction()
//    {
//        try {
////            $data = Msg::getUnreadNotice(5);
//            $data = Msg::noticeFinder()->unreadList(5);
//            return $this->sendJsonResponse($data);
//        } catch (\Exception $e) {
//            return $this->sendErrorResponse(500, '系统异常', $e->getMessage());
//        }
//    }

    /**
     * 获取未读消息及未处理任务
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by ck 20190213
     */
    public function getPreviewMsgAction()
    {
        try {
            $notice = Msg::noticeFinder()->unreadList(5);
            $task   = Msg::taskFinder()->unhandledList(5);
            return $this->sendJsonResponse(['notice' => $notice, 'task' => $task]);
        } catch (\Exception $e) {
            return $this->sendErrorResponse(500, '系统异常', $e->getMessage());
        }
    }

    /**
     * 根据消息id获取消息内容
     *
     * @param int $id
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by ck 20190122
     */
    public function readNoticeAction($id = 0)
    {
        if (!$id) {
            return $this->sendErrorResponse(400, '缺少消息id');
        }
        try {
//            $data = Msg::getNoticeInfo($id);
            $data = Msg::noticeFinder()->detail($id);
//            Msg::setNoticeRead($id);
            Msg::noticeOperator()->read($id);
            return $this->sendJsonResponse($data);
        } catch (\Exception $e) {
            return $this->sendErrorResponse(500, '系统异常', $e->getMessage());
        }
    }

    /**
     * 消息列表
     *
     * @return \Phalcon\Mvc\View
     *
     * create by ck 20190125
     */
    public function noticeAction()
    {
        $this->view->setTemplateBefore('listForm');
        Assets::easyUI('js/remind/notice.js');
        $listButton = [];
        $listButton[] = ['id' => 'bt_list_set_read', 'text' => '标记已读', 'icon' => 'fa-check'];
        $listButton[] = ['id' => 'bt_list_del', 'text' => '删除', 'icon' => 'fa-trash'];
        return $this->view->setVars([
            'List' => Template::createList('list', 'Remind_Notice', [
                'buttonToolbar' => Template::createButtonToolBar($listButton)
            ])
        ]);
    }

    /**
     * 获取消息列表
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by ck 20190125
     */
    public function getNoticeAction()
    {
        $page = $this->request->getPost('page', 'int', 1);
        $size = $this->request->getPost('size', 'int', 50);
        try {
//            $data = Msg::getNotice($page, $size);
            $data = Msg::noticeFinder()->list($page, $size);
            return $this->sendJsonResponse($data);
        } catch (\Exception $e) {
            return $this->sendErrorResponse(500, '系统异常', $e->getMessage());
        }
    }

    /**
     * 设置消息已读
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by ck 20190130
     */
    public function setNoticeReadAction()
    {
        $ids = $this->request->getPost('noticeIds', 'int');
        if (!$ids) {
            return $this->sendErrorResponse(400, '缺少消息id');
        }
        try {
//            Msg::setNoticeRead($ids);
            Msg::noticeOperator()->read($ids);
            return $this->sendSuccessResponse('操作成功');
        } catch (\Exception $e) {
            return $this->sendErrorResponse(500, '系统异常', $e->getMessage());
        }
    }

    /**
     * 删除消息
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by ck 20190130
     */
    public function delNoticeAction()
    {
        $ids = $this->request->getPost('ids', 'int');
        if (!$ids) {
            return $this->sendErrorResponse(400, '缺少消息id');
        }
        try {
//            Msg::delNotice($ids);
            Msg::noticeOperator()->del($ids);
            return $this->sendSuccessResponse('操作成功');
        } catch (\Exception $e) {
            return $this->sendErrorResponse(500, '系统异常', $e->getMessage());
        }
    }

    /**
     * 根据任务id获取内容
     *
     * @param int $id
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by ck 20190214
     */
    public function readTaskAction($id = 0)
    {
        if (!$id) {
            return $this->sendErrorResponse(400, '缺少任务id');
        }
        try {
            $data = Msg::taskFinder()->detail($id);
            Msg::taskOperator()->read($id);
            return $this->sendJsonResponse($data);
        } catch (\Exception $e) {
            return $this->sendErrorResponse(500, '系统异常', $e->getMessage());
        }
    }

    /**
     * 任务列表
     *
     * @return \Phalcon\Mvc\View
     *
     * create by ck 20190213
     */
    public function taskAction()
    {
        $this->view->setTemplateBefore('listForm');
        Assets::easyUI('js/remind/task.js');
        $listButton = [];
//        $listButton[] = ['id' => 'bt_list_set_read', 'text' => '标记已读', 'icon' => 'fa-check'];
        $listButton[] = ['id' => 'bt_list_del', 'text' => '删除', 'icon' => 'fa-trash'];
        return $this->view->setVars([
            'List' => Template::createList('list', 'Remind_Task', [
                'buttonToolbar' => Template::createButtonToolBar($listButton)
            ])
        ]);
    }

    /**
     * 获取任务列表
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by ck 20190213
     */
    public function getTaskAction()
    {
        $page = $this->request->getPost('page', 'int', 1);
        $size = $this->request->getPost('size', 'int', 50);
        try {
            $data = Msg::taskFinder()->list($page, $size);
            return $this->sendJsonResponse($data);
        } catch (\Exception $e) {
            return $this->sendErrorResponse(500, '系统异常', $e->getMessage());
        }
    }

    /**
     * 删除任务
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by ck 20190130
     */
    public function delTaskAction()
    {
        $ids = $this->request->getPost('ids', 'int');
        if (!$ids) {
            return $this->sendErrorResponse(400, '缺少消息id');
        }
        try {
            Msg::taskOperator()->del($ids);
            return $this->sendSuccessResponse('操作成功');
        } catch (\Exception $e) {
            return $this->sendErrorResponse(500, '系统异常', $e->getMessage());
        }
    }
}
